# -*- coding: utf8 -*-
from django.views import View
from django.views.generic.base import TemplateView
from django.urls import reverse
from django.shortcuts import redirect, render
from django.contrib.auth import authenticate, login, logout as django_logout


class Login(View):

    def get(self, request):
        return render(request, 'login.html', {})

    def post(self, request):
        args = request.POST
        user = authenticate(username=args.get('username'), password=args.get('password'))
        if user is not None:
            login(request, user)
            return redirect(reverse('index'))
        return redirect(reverse('login'))


def logout(request):
    django_logout(request)
    return redirect(reverse('index'))